package com.gardenguell.salad.ui.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.gardenguell.salad.R;
import com.gardenguell.salad.app.AppContext;
import com.gardenguell.salad.app.HandlerCenter;
import com.gardenguell.salad.data.cache.CacheService;
import com.gardenguell.salad.data.cache.MessageDBManager;
import com.gardenguell.salad.data.model.Unit;
import com.gardenguell.salad.util.ResUtil;

import java.util.List;

/**
 * Created by absurd on 16/5/28.
 */
public class ProgressLine extends LinearLayout implements View.OnClickListener{


    private ImageView mWarning;
    private View mProceedingLine;
    private FrameLayout mProgressLine;

    private Unit unit;
    private int unitNum;


    public ProgressLine(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressLine(Context context) {
        this(context, null);
    }


    public ProgressLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_progress_line, this, true);

        mProgressLine = (FrameLayout)findViewById(R.id.progress_line);
        mWarning = (ImageView)findViewById(R.id.warning);
        mProceedingLine = findViewById(R.id.proceeding_line);
        mWarning.setVisibility(View.INVISIBLE);
        mWarning.setOnClickListener(this);

    }



    public void setProceeding(double rate, int width){
        if(rate >= 1){
            mWarning.setVisibility(View.VISIBLE);
            mProceedingLine.setBackgroundDrawable(getResources().getDrawable(R.drawable.proceeding_line_round, null));
        }
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((int)(width * rate), FrameLayout.LayoutParams.MATCH_PARENT);
        mProceedingLine.setLayoutParams(params);
    }


    public void showWarning(int visibility){
        mWarning.setVisibility(visibility);
    }


    public void setUnit(Unit u){
        this.unit = u;
    }

    public void setUnitNum(int num){
        this.unitNum = num;
    }


    @Override
    public void onClick(View v) {

        Message msg = Message.obtain();
        msg.what = HandlerCenter.Message.CLICK_WARNING;
        msg.obj = unit;
        msg.arg1 = unitNum;
        HandlerCenter.getInstance().getHandler(HandlerCenter.Flag.UNIT_FRAGMENT)
                .sendMessage(msg);
    }
}
